const Handlebars = require('handlebars')

const renderTemplate = (template, data) => {
  return Handlebars.compile(template, {
    noEscape: true,
  })(data)
}
const renderTemplateMap = (resource) => {
  const data = {
    data: resource,
  }
  const template = `
    const map = {
        {{#each data}}
          '{{this.id}}': {
            fn: {{this.code}},
            mapping: {{ this.mapping}}
          },
        {{/each}}
  }
  ;(function (map) {
    function require(path) {
      const { fn,mapping } = map[path]
      const module = {
        exports: {},
      }
      const localRequire = (path) => {
        return require(mapping[path])
      }
      fn(localRequire, module, module.exports)
      return module.exports
    }
    require('0')
  })(map)
  `
  return renderTemplate(template, data)
}
module.exports = {
  renderTemplateMap,
}
